<template>
  <div class="login-container">
    <el-card class="box-card" shadow="always">
      <el-col span=11>
        <div style="display: table-cell; vertical-align: middle; text-align: center">
          <span class="left-title">扫码登录</span>
          <span class="left-title" style="color: #bfbfbf; font-size: 14px">使用微信“扫一扫”登录</span>
          <el-image src="https://pica.zhimg.com/36940066ff196785773634369b4290c8_r.jpg?source=1940ef5c"
            class="QR-img"></el-image>
          <span class="left-title"></span>
        </div>
      </el-col>

      <el-col span=1>
        <div class="line"></div>
      </el-col>

      <el-col span=11>
        <el-tabs v-model="activeName" :stretch="true" style="margin-left: 20px">
          <!-- 密码登录 -->
          <el-tab-pane label="密码登录" name="loginByPassword">
            <el-form ref="loginForm1" :model="loginForm1" :rules="loginRules1">
              <el-form-item prop="phone">
                <el-input v-model="loginForm1.phone" placeholder="请输入手机号" prefix-icon="el-icon-user"
                  style="margin-top: 30px;"></el-input>
              </el-form-item>
              <el-form-item prop="password">
                <el-input v-model="loginForm1.password" placeholder="请输入密码" prefix-icon="el-icon-lock"
                  show-password></el-input>
              </el-form-item>
            </el-form>
            <el-button type="primary" @click="loginByPasswd" class="login-button" :loading="loading">登录</el-button>
            <el-row>
              <el-col span=12>
                <el-button type="text" size="small" @click="gotoForget">忘记密码？</el-button>
              </el-col>
              <el-col span=12 align="right">
                <el-button type="text" size="small" @click="gotoRegister">还没注册？</el-button>
              </el-col>
            </el-row>
          </el-tab-pane>
          <!-- 验证码登录 -->
          <el-tab-pane label="短信登录" name="loginByCaptcha">
            <el-form ref="loginForm2" :model="loginForm2" :rules="loginRules2">
              <el-form-item prop="phone">
                <el-input v-model="loginForm2.phone" placeholder="请输入手机号" prefix-icon="el-icon-user"
                  style="margin-top: 30px"></el-input>
              </el-form-item>
              <el-form-item prop="captcha">
                <div style="display: flex; align-items: center">
                  <el-input v-model="loginForm2.captcha" placeholder="6 位短信验证码" prefix-icon="el-icon-lock"
                    style="width: 100%; margin-right: 10px"></el-input>
                  <GetCaptchaButton style="width: auto" :phone="loginForm2.phone"></GetCaptchaButton>
                </div>
              </el-form-item>
            </el-form>
            <el-button type="primary" @click="loginByCaptcha" class="login-button" :loading="loading">登录/注册</el-button>
          </el-tab-pane>
        </el-tabs>
      </el-col>
    </el-card>
  </div>
</template>

<script>
import Cookies from "js-cookie";
import GetCaptchaButton from "@/components/GetCaptchaButton.vue";
import { validatePassword, validatePhone, validateCaptcha } from "@/utils/validate";
import { loginByPassword, loginByCaptcha } from "@/api/auth/user";

export default {
  components: { GetCaptchaButton },
  data() {
    return {
      activeName: "loginByPassword",
      loginForm1: {
        phone: '',
        password: ''
      },
      loginForm2: {
        phone: '',
        captcha: ''
      },
      loginRules1: {
        phone: [{ required: true, trigger: 'blur', validator: validatePhone }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loginRules2: {
        phone: [{ required: true, trigger: 'blur', validator: validatePhone }],
        captcha: [{ required: true, trigger: 'blur', validator: validateCaptcha }]
      },
      loading: false
    }
  },
  methods: {
    loginByPasswd() {
      this.$refs.loginForm1.validate(valid => {
        if (valid) {
          this.loading = true
          loginByPassword(this.loginForm1).then(res => {
            if (res.data.code === 200) {
              Cookies.set('user', JSON.stringify(res.data.userDetailInfoVO))
              Cookies.set('user_token', JSON.stringify(res.data.tokenInfo))
              this.$router.push('/user/all')
              this.$message.success(res.data.message)
            } else {
              // this.$message.error(res.data.message)
            }
            this.loading = false
          })
        } else {
          return false
        }
      })
    },
    loginByCaptcha() {
      this.$refs.loginForm2.validate(valid => {
        if (valid) {
          this.loading = true
          loginByCaptcha(this.loginForm2).then(res => {
            if (res.data.code === 200) {
              Cookies.set('user', JSON.stringify(res.data.userDetailInfoVO))
              Cookies.set('user_token', JSON.stringify(res.data.tokenInfo))
              this.$router.push('/user/all')
              this.$message.success(res.data.message)
            } else {
              // this.$message.error(res.data.message)
            }
            this.loading = false
          })
        } else {
          return false
        }
      })
    },
    gotoForget() {
      this.$router.push('/forget')
    },
    gotoRegister() {
      this.$router.push('/register')
    }
  }
}
</script>

<style lang="scss" scoped>
$dark_gray: #bfbfbf;
$white: #ffffff;

.text {
  font-size: 14px;
}

.item {
  padding: 18px 0;
}

.left-title {
  display: block;
  text-align: center;
  margin-top: 5%;
  margin-bottom: 10px;
  font-size: 20px;
}

.QR-img {
  width: 60%;
  height: 60%;
  text-align: center;
  border: solid 1px $dark_gray;
}

.line {
  width: 15px;
  height: 400px;
  border-right: solid $dark_gray 1px;
  margin-top: 10px;
  margin-bottom: 10px;
}

.login-container {
  position: relative;
  background: url("@/assets/image/background.png") no-repeat;
  height: 100%;
  width: 100%;
  overflow: hidden;
  background-size: cover;
  margin: 0;
  padding: 0;
}

.box-card {
  position: absolute;
  width: 750px;
  height: 450px;
  top: 0;
  bottom: 0;
  left: 40%;
  right: 30%;
  margin: auto;
  background-color: $white;
  border-radius: 15px;
}

.login-button {
  margin-top: 30px;
  margin-bottom: 5px;
  width: 100%;
}</style>
